import org.apache.commons.lang.StringUtils;

import java.io.BufferedReader;
import java.io.InputStreamReader;

class CopyOfEarthSpace {
    //char [] spaceword;
    String spaceword;
    String[] alias;
    int[] aliaskey;
    int alias_count;
    int[] earthword;
    String[] coins;
    double[] credits;
    int coin_count;

    public static void main(String args[]) throws Exception {
        //String source;
        //BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        //System.out.println("Welcome, spaceling : What's the amount?");
        //source=br.readLine();
        CopyOfEarthSpace e = new CopyOfEarthSpace();
        int choice;
        while (true) {
            System.out.println("Choices : ");
            System.out.println("1. Set spacename to Roman numeral");
            System.out.println("2. Define coin-credit values");
            System.out.println("3. Ask a question");
            System.out.print("You choice : ");
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            try {
                choice = Integer.parseInt(br.readLine());
            } catch (NumberFormatException e1) {
                System.out.println("Numbers only");

                continue;
            }
            switch (choice) {
                case 1:
                    System.out.print("Enter here : ");
                    e.choice1();
                    break;
                case 2:
                    System.out.print("Enter here : ");
                    e.choice2();
                    break;
                case 3:
                    e.choice3();
                    /*int s1=e.break_string("pish tegj glob glob");
                     System.out.println(s1);
                     double s2=e.calc_credit("glob prok","Silver");
                     if(Double.compare(s2, 0.0)==0)
                         System.out.println("I have no idea what you are talking about");
                     else
                       System.out.println(s2);
                     double s3=e.calc_credit("glob prok","Gold");
                     double s4=e.calc_credit("glob prok","Iron");
                     //System.out.println(s2);
                     System.out.println(s3);
                     System.out.println(s4);
                     */
                    break;
                case 4:
                    System.exit(1);
                    break;
                default:
                    System.out.println("Wrong input");
                    break;
            }
        }


        /*
           1. Set spacename to roman numeral
           2. Define the credits
           3. Query the system

           */
        //int num=e.convert(source);
        //System.out.println("Earth value =" + num);
        //e.add_alias("glob", 'I');
        //e.add_alias("prok", 'V');
        //e.add_alias("pish", 'X');
        //e.add_alias("tegj", 'L');
        //e.assign_credits("glob glob", "Silver", 34);
        //e.assign_credits("glob prok", "Gold", 57800);
        //e.assign_credits("pish pish","Iron", 3910);
        // AND NOW FOR THE ANSWERS :
        //int s1=e.break_string("pish tegj glob glob");
        //System.out.println(s1);
        //double s2=e.calc_credit("glob prok","Silver");
        //if(Double.compare(s2, 0.0)==0)
        //	System.out.println("I have no idea what you are talking about");
        //else
        //  System.out.println(s2);
        //double s3=e.calc_credit("glob prok","Gold");
        //double s4=e.calc_credit("glob prok","Iron");
        //System.out.println(s2);
        //System.out.println(s3);
        //System.out.println(s4);
    }

    void choice1() throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String source;
        source = br.readLine();
        String sentence_broken[] = source.split(" ");
        if (sentence_broken.length != 3)
            System.out.println("Something is not right!");
        else {
            if (sentence_broken[1].equals("is")) {
                if (sentence_broken[2].length() == 1) {
                    char t = sentence_broken[2].charAt(0);
                    if (spaceword.indexOf(t) < 0) {
                        System.out.println("I don't understand what you are saying");
                    } else {
                        add_alias(sentence_broken[0], t);
                        System.out.println(sentence_broken[0] + "<---->" + t);
                        //e.assign_credits("glob glob", "Silver", 34);

                    }
                } else
                    System.out.println("I don't understand what you are saying");
            } else
                System.out.println("I don't understand what you are saying.");
        }
    }

    void choice2() throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String source;
        int i, index = -1;
        int cred_count;
        String coin_value = new String();
        source = br.readLine();
        String sentence_broken[] = source.split(" ");
        for (i = 0; i < sentence_broken.length; i++) {
            //System.out.println("adsf");
            //find "is"
            if (sentence_broken[i].equals("is")) {
                index = i;
                break;
            }

        }
        if (index == -1)
            System.out.println("I don't understand what you're saying");
        else {
            //Now check if the last word is "Credits"
            //So if index is where the "is" is, then index+2 should have the word "Credits"
            if (sentence_broken[i + 2].equals("Credits")) {
                //Proceed to break up the sentence
                if (StringUtils.isNumeric(sentence_broken[i + 1])) {
                    cred_count = Integer.parseInt(sentence_broken[i + 1]);
                    //coin_value=sentence_broken[i-1];
                    coin_value = sentence_broken[0];
                    for (int j = 1; j <= i - 2; j++)
                        coin_value = coin_value + " " + sentence_broken[j];
                    System.out.println(coin_value);
                    assign_credits(coin_value, sentence_broken[i - 1], cred_count);
                }

            } else
                System.out.println("I don't understand what you're saying");


        }

    }

    void choice3() throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String source;
        System.out.println("Enter here : ");
        source = br.readLine();
        String sentence_broken[] = source.split(" ");
        // Parse the line
        //1. Is the first word "How"
        //2. Is the second word "much" or "many"
        if (sentence_broken[0].equals("how")) {
            if (sentence_broken[1].equals("much")) {
                if (sentence_broken[2].equals("is")) {
                    String check = new String();
                    check = sentence_broken[3];
                    for (int i = 4; i < sentence_broken.length - 1; i++)
                        check = check + " " + sentence_broken[i];
                    System.out.println(check);
                    int value = break_string(check);

                    System.out.println(value);
                    // Use the function call like this : break_string("pish tegj glob glob");
                }
            } else if (sentence_broken[1].equals("many")) {
                if (sentence_broken[2].equals("Credits") && sentence_broken[3].equals("is")) {

                    int last = sentence_broken.length - 1;
                    if (sentence_broken[last].equals("?")) {
                        last = last - 1;
                    }
                    String coin = sentence_broken[last];
                    String value = new String();
                    value = sentence_broken[4];
                    for (int i = 5; i < last; i++) {
                        value = value + " " + sentence_broken[i];
                    }
                    System.out.println(value + " " + coin);
                    double t = calc_credit(value, coin);
                    System.out.println(t);
                } else
                    System.out.println("NO idea!");
            } else
                System.out.println("Dont know what you're talking about....");
        } else {
            System.out.println("No idea what you're talking about...");
        }
    }

    CopyOfEarthSpace() {
        alias = new String[20];
        aliaskey = new int[20];
        alias_count = 0;
        spaceword = "IVXLCDM";
        earthword = new int[]{1, 5, 10, 50, 100, 500, 1000};
        coins = new String[20];
        coin_count = 0;
        credits = new double[20];
        //earthword=earthword1;
    }

    double calc_credit(String space_name, String coin_name) throws Exception {
        int coin_count_local = break_string(space_name);
        //Find the corresponding coin name :
        int i, index = -1;
        for (i = 0; i < coins.length; i++) {
            if (coin_name.equals(coins[i])) {
                index = i;
                break;
            }
        }
        //i holds the index now
        if (index != -1)
            return (coin_count_local * credits[index]);
        else
            return 0;
        //return 0;
        //So... if it doesn't find an existing "coin" name,  then it should return a zero.
        // But remember, it's a double. so I don't know if I can compare it as 0.0 or as 0...
    }

    void assign_credits(String space_name, String coin_name, int cred_count) throws Exception {
        int coin_count_here = break_string(space_name);
        double single_coin_credit_val = (double) cred_count / coin_count_here;
        coins[coin_count] = coin_name;
        credits[coin_count] = single_coin_credit_val;
        //System.out.println("--->> "+credits[coin_count]+ "   "+coins[coin_count] + " <<-----");
        coin_count++;
    }

    void add_alias(String new_alias, char real_name) throws Exception {
        alias[alias_count] = new_alias;
        aliaskey[alias_count] = match(real_name);
        //System.out.println(alias[alias_count]+" = "+aliaskey[alias_count]);
        alias_count = alias_count + 1;
    }

    int break_string(String input) throws Exception {
        String[] pieces = input.split("\\s");
        String converted = new String();
        for (int i = 0; i < pieces.length; i++) {
            //System.out.println(pieces[i]);
            converted = converted + search_alias(pieces[i]);
        }
        //System.out.println(converted);
        int t = convert(converted);
        //System.out.println(t);
        return t;
    }

    char search_alias(String resolve_alias) {
        for (int i = 0; i < alias.length; i++) {
            if (resolve_alias.equals(alias[i])) {
                return spaceword.charAt(aliaskey[i]);
            }
        }
        return 'z';
    }

    int convert(String source) throws Exception {

        int temp;
        int sum = 0;
        int prev = 10;
        int count;
        char check;
        if (!source.matches("[IVXLCDM]+")) {
            System.out.println("Invalid String....");
            return 0;
        }

        count = 1;
        for (int i = 0; i < source.length(); i++) {
            temp = match(source.charAt(i));
            if (prev == temp) {
                check = spaceword.charAt(temp);
                if (check == 'D' || check == 'V' || check == 'L') {
                    System.out.println("Invalid Num");
                    return 0;
                } else {
                    count++;
                    if (count > 3) {
                        System.out.println("Invalid Num");
                        return 0;
                    }

                }
            } else
                count = 1;
            if (prev < temp) {
                //Add restrictions here before doing the subtraction
                // I can only be subtracted from V and X
                // X can be only subtracted from L and C
                // C can be only subtracted from D and M
                // V, L and D can never be subtracted.
                if (spaceword.charAt(temp) == 'I') {
                    if (!(spaceword.charAt(prev) == 'V' || spaceword.charAt(prev) == 'X')) {
                        System.out.println("Wrong");
                        return 0;
                    }
                }
                if (spaceword.charAt(temp) == 'X') {
                    if (!(spaceword.charAt(prev) == 'L' || spaceword.charAt(prev) == 'C')) {
                        System.out.println("Wrong");
                        return 0;
                    }

                }
                if (spaceword.charAt(temp) == 'C') {
                    if (!(spaceword.charAt(prev) == 'D' || spaceword.charAt(prev) == 'M')) {
                        System.out.println("Wrong");
                        return 0;
                    }
                }
                if (spaceword.charAt(prev) == 'L' || spaceword.charAt(prev) == 'V' || spaceword.charAt(prev) == 'D') {
                    System.out.println("Wrong");
                    return 0;

                }
                sum = sum - earthword[prev];
                sum = sum - earthword[prev];
                sum = sum + earthword[temp];
            } else

                sum = sum + earthword[temp];
            prev = temp;
            //System.out.println(sum);
        }

        //System.out.println("The converted value = "+sum);
        return sum;
    }

    int match(char find) throws Exception {
        return spaceword.indexOf(find);
    }
    /*double coinset(int coin_count,int cred_count,int find_coin_cnt)
     {
         double single_coin_credit_val=(double)cred_count/coin_count;
         double ret_val=single_coin_credit_val*find_coin_cnt;
         return ret_val;

     }*/
}

